﻿using System;
using System.Collections.Generic;
using Dict.Config;

namespace Battle.Engine.LogicModel
{
    //血量百分比条件，例如：血量值小于一半的目标（即目标当前的血量百分比小于50%）： type:perhp,src1:3,src2_val:0,op:5,src2:1,src2_val:50
    public class HpPercentCondition : BuffCondition
    {
        public override void init(FActor actor, string args)
        {
            base.init(actor, args);
            
        }


        public override bool IsConditionTure(FActor target)
        {
            double val1 = 0;
            double val2 = 0;
            //读取数值1
            if ((ConditionParam)this.src1 == ConditionParam.SelfCur)
            {
                if (this.type.Equals(TableFactorConst.hp))
                    val1 = this.actor.GetValueByType(TableFactorConst.hp) / this.actor.GetValueByType(TableFactorConst.maxHp);
                else if(this.type.Equals(TableFactorConst.totalLeftHp))
                    val1 = this.actor.GetValueByType(TableFactorConst.totalLeftHp) / this.actor.GetValueByType(TableFactorConst.totalMaxHp);

            }
            else if ((ConditionParam)this.src1 == ConditionParam.TargetCur)
            {
                if (this.type.Equals(TableFactorConst.hp))
                    val1 = target.GetValueByType(TableFactorConst.hp) / target.GetValueByType(TableFactorConst.maxHp);
                else if (this.type.Equals(TableFactorConst.totalLeftHp))
                    val1 = target.GetValueByType(TableFactorConst.totalLeftHp) / this.actor.GetValueByType(TableFactorConst.totalMaxHp);
            }
            else
            {
                val1 = this.src1_val / 100;
            }

            //读取数值2
            if ((ConditionParam)this.src2 == ConditionParam.SelfCur)
            {
                if (this.type.Equals(TableFactorConst.hp))
                    val2 = this.actor.GetValueByType(TableFactorConst.hp) / this.actor.GetValueByType(TableFactorConst.maxHp);
                else if (this.type.Equals(TableFactorConst.totalLeftHp))
                    val2 = this.actor.GetValueByType(TableFactorConst.totalLeftHp) / this.actor.GetValueByType(TableFactorConst.totalMaxHp);
            }
            else if ((ConditionParam)this.src2 == ConditionParam.TargetCur)
            {
                if (this.type.Equals(TableFactorConst.hp))
                    val2 = target.GetValueByType(TableFactorConst.hp) / target.GetValueByType(TableFactorConst.maxHp);
                else if (this.type.Equals(TableFactorConst.totalLeftHp))
                    val2 = target.GetValueByType(TableFactorConst.totalLeftHp) / this.actor.GetValueByType(TableFactorConst.totalMaxHp);
            }
            else
            {
                val2 = this.src2_val / 100;
            }

            //处理判定
            switch ((ConditionOP)this.op)
            {
                case ConditionOP.More:
                    if (val1 > val2)
                        return true;
                    break;
                case ConditionOP.MoreAndEqual:
                    if (val1 >= val2)
                        return true;
                    break;
                case ConditionOP.Equal:
                    if (val1 == val2)
                        return true;
                    break;
                case ConditionOP.LessAndEqual:
                    if (val1 <= val2)
                        return true;
                    break;
                case ConditionOP.Less:
                    if (val1 <= val2)
                        return true;
                    break;
                case ConditionOP.Remainder:
                    if (val1 % val2 == this.op_val)
                        return true;
                    break;
                default:
                    break;
            }
            return false;
        }
    }
}
